package china.accountinghelper.activity;

import java.util.Calendar;

import com.xdroid.base.ActivityDialogBase;
import com.xdroid.util.RegexUtil;

import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessCategory;
import china.wc.tallyhelper.model.ModelCategory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityCategoryAddAndEdit extends ActivityDialogBase
{
    // ModelCategory的业务类
    private BusinessCategory mBusinessCategory;
    // ModelCategory对象
    private ModelCategory    mHandlingModel;

    private EditText         mDialogCategoryAddAndEdit_EditText_Name;
    private Button           mDialogCategoryAddAndEdit_Button_OK;
    private Button           mDialogCategoryAddAndEdit_Button_Cancel;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_category_add_and_edit);

        initFields();
        initViews();
        initListeners();
        bindData();
        register();

        // 设置标题
        if (mHandlingModel.getID() <= 0) {
            setTitle(R.string.title_category_add);
        } else {
            setTitle(R.string.title_category_edit);
            setName();
        }

    }

    @Override
    public void onViewClick(View v)
    {
        if (v.equals(mDialogCategoryAddAndEdit_Button_OK)) {
            if (!nameIsOk()) {
                showToastShort(R.string.message_category_check_name);
            } else {
                setModelPerson();
                boolean _Result = mBusinessCategory.insertOrUpdateModel(mHandlingModel);
                setResultCode(_Result);
                finish();
            }
    
        } else if (v.equals(mDialogCategoryAddAndEdit_Button_Cancel)) {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override
    protected void initFields()
    {
        mBusinessCategory = new BusinessCategory();
        mHandlingModel = initHandlingModel();
    }

    @Override
    protected void initViews()
    {
        mDialogCategoryAddAndEdit_EditText_Name = (EditText) findViewById(R.id.DialogCategoryAddAndEdit_EditText_Name);
        mDialogCategoryAddAndEdit_Button_OK = (Button) findViewById(R.id.DialogCategoryAddAndEdit_Button_OK);
        mDialogCategoryAddAndEdit_Button_Cancel = (Button) findViewById(R.id.DialogCategoryAddAndEdit_Button_Cancel);
    }

    @Override
    protected void initListeners()
    {
        mDialogCategoryAddAndEdit_Button_OK.setOnClickListener(this);
        mDialogCategoryAddAndEdit_Button_Cancel.setOnClickListener(this);
    }

    @Override
    protected void bindData()
    {
    }

    @Override
    protected void register()
    {
    }

    /**
     * 若Intent中包含ModelCategory对象，则将该对象赋值给mHandlingModel。
     * 否则新建一个CategoryId为0的ModelCategory对象，然后赋值给mHandlingModel。
     * 
     * @return ModelCategory对象
     */
    private ModelCategory initHandlingModel()
    {
        ModelCategory _ModelCategory = (ModelCategory) getIntent().getSerializableExtra(ActivityCategory.EDITING_MODEL_OBJECT);

        if (_ModelCategory == null) {
            mHandlingModel = new ModelCategory();
            mHandlingModel.setID(0);
        } else {
            mHandlingModel = _ModelCategory;
        }

        return mHandlingModel;
    }

    /**
     * 将当前页面的Name，外加当前时间，赋值给mHandlingModel。
     * 
     * @return mHandlingModel
     */
    private ModelCategory setModelPerson()
    {
        mHandlingModel.setName(getName());
        mHandlingModel.setCreateTime(Calendar.getInstance());

        return mHandlingModel;
    }

    /**
     * 检查当前界面的Name是否合法
     * 
     * @return 合法返回true，不合法返回false。
     */
    private boolean nameIsOk()
    {
        String _Name = getName();
        return RegexUtil.isUserNameChn(_Name);
    }

    /**
     * 从界面上的文本框中，得到类别名称
     * 
     * @return 类别名称
     */
    private String getName()
    {
        return mDialogCategoryAddAndEdit_EditText_Name.getText().toString().trim();
    }

    /**
     * 将mHandlingModel的Name，显示到界面上的文本框中
     */
    private void setName()
    {
        if (mHandlingModel.getID() <= 0) {
            mDialogCategoryAddAndEdit_EditText_Name.setText(null);
        } else {
            mDialogCategoryAddAndEdit_EditText_Name.setText(mHandlingModel.getName());
        }

    }

    private void setResultCode(boolean pResult)
    {
        if (pResult == true) {
            setResult(ActivityCategory.RESULT_OK);
        } else {
            setResult(ActivityCategory.RESULT_ERR);
        }
    }

}
